package com.hzjh.base.utils.other

import android.annotation.SuppressLint
import android.provider.Settings
import com.blankj.utilcode.util.Utils
import com.hzjh.base.utils.stroage.SKStorage
import java.util.UUID

object KtDeviceUtils {
    /**
     * 所有的deviceID都要从这里取 设备id
     * @param context
     * @return
     */
    private var deviceId = ""
    private const val KEY_DEVICE_ID= "key_device_id"
    @SuppressLint("HardwareIds")
    fun getDeviceId(): String {
        return deviceId.ifEmpty {
            var androidID = SKStorage.getString(KEY_DEVICE_ID, "")
            deviceId =
                if (androidID.isNullOrEmpty()) {
                    val androidID =
                        Settings.Secure.getString(
                            Utils.getApp().contentResolver,
                            Settings.Secure.ANDROID_ID
                        )
                    if (androidID.isNullOrEmpty()) {
                        UUID.randomUUID().toString()
                    } else {
                        try {
                            Md5Utils.md5(androidID)
                        } catch (e: java.lang.Exception) {
                            e.printStackTrace()
                            "$androidID"
                        }
                    }
                } else {
                    androidID
                }
            SKStorage.put(KEY_DEVICE_ID, deviceId)
            return deviceId
        }
    }
}